import JSEncrypt from "jsencrypt/bin/jsencrypt.min";
// 密钥对生成 http://web.chacuo.net/netrsakeypair; 把下面生成的公钥、私钥换成自己生成的即可
const publicKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA16T1uWwP/jxyfLMk5LzR
i5jyOiL/iq7a76TgZxN2npwPmVsEZJHeWY5CDcF6BYEh5d1BYVHbhhH0FZj4PRQn
FTCGp7LfbWna6M/H1HNMET4aucOOFxbJaVn/AHRFRSqIBtvXdhCp5UmbFdKJSL2O
s5a8Dvvl8TC0MKeN9FA/r8mhMkbF6S30Piwr2ZsFskWIysHWuqJ2efJbWh2QdAd8
QVgiHzd3ZlypFTXzbuOZhrAj8WG2liUAomPWuD1BaIoRBOs797o0XcZeelfaSg65
d6JNnoO+hh9RrWaU8W7jjoiYQpYT2dIDxrzkupVTM0dbWZNFAZ91TlZvxigbntgJ
YQIDAQAB
-----END PUBLIC KEY-----
`; //生成的公钥
const privateKey = `-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDXpPW5bA/+PHJ8
syTkvNGLmPI6Iv+KrtrvpOBnE3aenA+ZWwRkkd5ZjkINwXoFgSHl3UFhUduGEfQV
mPg9FCcVMIanst9tadroz8fUc0wRPhq5w44XFslpWf8AdEVFKogG29d2EKnlSZsV
0olIvY6zlrwO++XxMLQwp430UD+vyaEyRsXpLfQ+LCvZmwWyRYjKwda6onZ58lta
HZB0B3xBWCIfN3dmXKkVNfNu45mGsCPxYbaWJQCiY9a4PUFoihEE6zv3ujRdxl56
V9pKDrl3ok2eg76GH1GtZpTxbuOOiJhClhPZ0gPGvOS6lVMzR1tZk0UBn3VOVm/G
KBue2AlhAgMBAAECggEBAMGEK28mZoPEQwlcyjLqrzETI+zAE4qkt5xu3WIiuURE
0ErSvvJxZJQBcY1ent3psD3TfCSVjOaXne7zw3EscJY3Yd+ESbSqVeSu8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ZRUK5wrPbEk9kQnzVzdv00RcDwpMmfUwT
kJf2EytC337ThxX0t8EZYUex
-----END PRIVATE KEY-----`; // 私钥

// 加密
export function encrypt(txt) {
  const encryptor = new JSEncrypt();
  encryptor.setPublicKey(publicKey); // 设置公钥
  return encryptor.encrypt(txt); // 对数据进行加密
}

// 解密
export function decrypt(txt) {
  const encryptor = new JSEncrypt();
  encryptor.setPrivateKey(privateKey); // 设置私钥
  return encryptor.decrypt(txt); // 对数据进行解密
}
